package com.edol.painting.painting;

import com.edol.painting.utils.PainterUtils;

import java.util.ArrayList;

public class Route extends ArrayList<Particle> {
	
	private static final long serialVersionUID = 2593521039063107713L;

	public static final int ROUTE_BEGIN_INDEX = 3;
	
	public long mDrawTime = 0;
	
	public Route() {
		super();
	}
	
	public Route(int size) {
		super(size);
	}
	
	public static Route calculateSmoothLinePoints(Particle p0, Particle p1, Particle p2, float segmentDistance) {
		Route route = new Route();
		if (p0 != null && p1 != null && p2 != null) {
			
			float midPoint1X = (p1.x + p0.x) / 2;
			float midPoint1Y = (p1.y + p0.y) / 2;
			float midPoint2X = (p1.x + p2.x) / 2;
			float midPoint2Y = (p1.y + p2.y) / 2;
			float midPoint1Width = (p1.width + p0.width) / 2;
			float midPoint2Width = (p1.width + p2.width) / 2;
			float midPoint1Alpha = (p1.alpha + p0.alpha) / 2;
			float midPoint2Alpha = (p1.alpha + p2.alpha) / 2;

			float distance = PainterUtils.getDistance(midPoint1X, midPoint1Y, midPoint2X, midPoint2Y);
			segmentDistance = Math.max(1, Math.min(distance, segmentDistance));
			int numOfSegment =  (int)(distance / segmentDistance);
			
			float t = 0;
			float step = 1.0f / numOfSegment;
			float deltaT = 0;
			for (int j = 0; j < numOfSegment; j++) {
				Particle p = new Particle();
				deltaT = 1 - t;
				p.x = midPoint1X * deltaT * deltaT + p1.x * 2 * deltaT * t + midPoint2X * t * t;
				p.y = midPoint1Y * deltaT * deltaT + p1.y * 2 * deltaT * t + midPoint2Y * t * t;
				p.width = midPoint1Width * deltaT * deltaT + p1.width * 2 * deltaT * t + midPoint2Width * t * t;
				p.alpha = (int) (midPoint1Alpha * deltaT * deltaT + p1.alpha * 2 * deltaT * t + midPoint2Alpha * t * t);

				//p.x = deltaT * deltaT * p0.x + 2 * t *deltaT * p1.x + t * t * p2.x;
				//p.y = deltaT * deltaT * p0.y + 2 * t *deltaT * p1.y + t * t * p2.y;
				//p.width = deltaT * deltaT * p0.width + 2 * t *deltaT * p1.width + t * t * p2.width;
				//p.alpha = (int)(deltaT * deltaT * p0.alpha + 2 * t *deltaT * p1.alpha + t * t * p2.alpha);
				route.add(p);
				t += step;
			}
		}
		return route;
	}
	
	public static Route calculateLinePoints(Particle p0, Particle p1, float segmentDistance) {
		Route route = new Route();
		if (p0 != null && p1 != null) {
			float distance = PainterUtils.getDistance(p0.x, p0.y, p1.x, p1.y);
			int numOfSegment = (int) (distance / segmentDistance + 1);
			float t = 0;
			float step = 1.0f / numOfSegment;
			float deltaT = 0;
			for (int i = 0; i < numOfSegment; i++) {
				Particle p = new Particle();
				deltaT = 1 - t;
				p.x = p0.x * deltaT + t * p1.x;
				p.y = p0.y * deltaT + t * p1.y;
				p.width = p0.width * deltaT + t * p1.width;
				p.alpha = (int) (p0.alpha * deltaT + t * p1.alpha);
				route.add(p);
				t += step;
			}
		}
		return route;
	}
}
